<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

function generate_product_time($campaign_start_time, $order) {
    $hour = 60*60;
    $vote_start_time = $campaign_start_time + (($order - 1) * (24 * $hour));
    $vote_end_time = $vote_start_time + (24 * $hour);
    $sell_start_time = $vote_end_time;
    $sell_end_time = $sell_start_time + (24 * $hour);
    return array(
        'vote_start_time' => $vote_start_time,
        'vote_end_time' => $vote_end_time,
        'sell_start_time' => $sell_start_time,
        'sell_end_time' => $sell_end_time
    );
}

function stupid_product_output($product) {
    $CI =& get_instance();
    $CI->load->model('setting_model');
    $campaign_start_time = $CI->setting_model->get_setting_val('campaign_start_time');
    $campaign_start_time = strtotime($campaign_start_time);
    $product_time = generate_product_time($campaign_start_time, $product['product_order']);
    
    $product['product_high_discount_picture'] = '/app/'.$product['product_high_discount_picture'];
    $product['product_low_discount_picture'] = '/app/'.$product['product_low_discount_picture'];
    $product['product_sharing_picture'] = '/app/'.$product['product_sharing_picture'];
    $product['product_pre_vote_picture'] = '/app/'.$product['product_pre_vote_picture'];
    $product_output = array();
    $product_output['product_id'] = $product['product_id'];
    $product_output['product_order'] = $product['product_order'];
    $now = time();
    // 等待投票
    if ($product_time['vote_start_time'] > $now) {
        $product_output['state'] = 'pre_vote';
        $product_output['time_left'] = $product_time['vote_start_time'] - $now;
        $product_output['product_picture'] = $product['product_pre_vote_picture'];
    // 我要投票
    } else if ($product_time['vote_end_time'] > $now) {
        $product_output['product_high_discount'] = $product['product_high_discount'];
        $product_output['product_low_discount'] = $product['product_low_discount'];
        $product_output['product_picture'] = $product['product_high_discount_picture'];
        $product_output['product_sharing_picture'] = $product['product_sharing_picture'];
        $product_output['product_link'] = $product['product_full_price_link'];
        $product_output['product_full_price_link'] = $product['product_full_price_link'];
        $product_output['product_vote_count'] = $product['product_vote_count'];
        $product_output['product_share_count'] = $product['product_share_count'];
        $product_output['product_required_votes'] = $product['product_required_votes'];
        $product_output['state'] = 'in_vote';
        $product_output['time_left'] = $product_time['vote_end_time'] - $now;
    // 我要抢购
    } else if ($product_time['sell_end_time'] > $now) {
        if ($product['product_required_votes'] > $product['product_vote_count']) {
            $product_output['product_discount'] = $product['product_low_discount'];
            $product_output['product_link'] = $product['product_low_discount_link'];
            $product_output['product_picture'] = $product['product_low_discount_picture'];
        } else {
            $product_output['product_discount'] = $product['product_high_discount'];
            $product_output['product_link'] = $product['product_high_discount_link'];
            $product_output['product_picture'] = $product['product_high_discount_picture'];
        }
        $CI->load->model('vote_model');
        if (!$CI->user->id || !$CI->vote_model->has_vote($CI->user->id, $product['product_id'])) {
            $product_output['product_link'] = '';
        }
        $product_output['product_vote_count'] = $product['product_vote_count'];
        $product_output['product_share_count'] = $product['product_share_count'];
        $product_output['product_required_votes'] = $product['product_required_votes'];
        $product_output['state'] = 'in_sell';
        $product_output['time_left'] = $product_time['sell_end_time'] - $now;
    // 抢购结束
    } else {
        if ($product['product_required_votes'] > $product['product_vote_count']) {
            $product_output['product_discount'] = $product['product_low_discount'];
            $product_output['product_picture'] = $product['product_low_discount_picture'];
        } else {
            $product_output['product_discount'] = $product['product_high_discount'];
            $product_output['product_picture'] = $product['product_high_discount_picture'];
        }
        $product_output['product_link'] = '';
        $product_output['product_vote_count'] = $product['product_vote_count'];
        $product_output['product_share_count'] = $product['product_share_count'];
        $product_output['product_required_votes'] = $product['product_required_votes'];
        $product_output['state'] = 'end_sell';
        $product_output['time_left'] = 0;
    }
    $product_output['campaign_date'] = date('Y-m-d', $campaign_start_time+((60*60*24)*($product['product_order']-1)));
    return $product_output;
}